package com.xyw.code.study20200812.study3.test1;

import lombok.Data;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/8/13
 * Time: 上午9:30
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
public class LockThreadTest {
    public static String lock = "aaa";

    public static void main(String[] args) {
        Inttest inttest = new Inttest();
        MyRunnable myRunnable1 = new MyRunnable(inttest);
        MyRunnable myRunnable2 = new MyRunnable(inttest);
        MyRunnable myRunnable3 = new MyRunnable(inttest);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(3,3,0, TimeUnit.SECONDS,new LinkedBlockingQueue<>());
        executor.submit(myRunnable1);
        executor.submit(myRunnable2);
        executor.submit(myRunnable3);
    }

}
@Data
class Inttest{
    private int i = 0;
}
class MyRunnable implements Runnable{
    private Inttest i;

    public MyRunnable(Inttest i){
       this.i = i;
    }

    @Override
    public void run() {
        synchronized(LockThreadTest.lock){
            while (i.getI()<100){
                System.out.println(i);
                i.setI(i.getI()+1);;
            }
        }
    }
}